package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Job;
import Database.DatabaseConnector;


@WebServlet("/DeleteClientServlet")
public class DeleteClientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DatabaseConnector dbConnect = new DatabaseConnector();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		String busName = (String) session.getAttribute("username");
		String ID = (String) req.getParameter("clientID");
		
		
		long clientID = Long.parseLong(ID);
		JSONObject result = new JSONObject();

		ArrayList<Job> jobList = dbConnect.getClientsJobs(clientID, busName);
		System.out.println("size "+ jobList.size());
		
		for(Job job : jobList)
		{
			System.out.println("removeJob");
			long jobID = job.getJobID();
			dbConnect.removeJob(jobID);
		}
		dbConnect.removeClient(clientID);
		try {
			result.put("success", true);
			res.getWriter().print(result);
			res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}


}
